<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateChatTable extends Migration
{
    /**
     * Run the migrations.
     * 部门负责人设置表
     *
     * @return void
     */
    public function up()
    {

        //群聊组的定义 1. 全体群聊就不用建立多对多表了 直接遍历所有人就行了 2. 多人多部门群聊需要把人加进来 3. 部门内群聊就是通知部门里面的所有人
        Schema::create('chat_group', function (Blueprint $table) {
            $table->Increments('id');
            $table->string('name')->comment('群聊名称')->unique();
            $table->unsignedInteger('employee_id')->comment('群聊创建者');
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('no action');
            $table->timestamps();
        });
        //群聊组对应的部门 适用于 chat_group的 type 3
        Schema::create('chat_employ_department', function (Blueprint $table) {
            $table->Increments('id');
            $table->unsignedInteger('chat_group_id')->comment('部门群聊模式使用该表');
            $table->foreign('chat_group_id')->references('id')->on('chat_group')->onDelete('no action');
            $table->unsignedInteger('employ_department_id')->comment('对应的组织');
            $table->foreign('employ_department_id')->references('id')->on('departments')->onDelete('no action');
            $table->unique(['chat_group_id','employ_department_id'],'c_e');
            $table->timestamps();
        });

        //群聊组对应的人 适用于 chat_group的 type 2
        Schema::create('chat_employ_users', function (Blueprint $table) {
            $table->Increments('id');
            $table->unsignedInteger('chat_group_id')->comment('部门群聊模式使用该表');
            $table->foreign('chat_group_id')->references('id')->on('chat_group')->onDelete('no action');
            $table->unsignedInteger('employee_id')->comment('对应的组织');
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('no action');
            $table->unique(['chat_group_id','employee_id'],'c_e');
            $table->timestamps();
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('chat_employ_users');
        Schema::dropIfExists('employ_department_id');
        Schema::dropIfExists('chat_group');
    }
}
